/**
 * [OElove] (C)2010-2099 OELOVE.COM Inc. X
 * Email: service@phpcoo.com ,phpcoo@qq.com
 * This is NOT a freeware, use is subject to license terms
 * $ Last update 2016/11/24 by CL $
*/
var boundx, boundy, flip_obj;
$(function(){

	//上传照片
	$(document).on("change", "[f='upload_img']", function(){
		$file = $(this).attr("id");
		$url = $(this).attr("data-url");

		$obj_tips = $("#"+$file+"_tips");
		if ($obj_tips.length > 0) {
			$obj_tips.html("");
		}

		$obj_loading = $("#"+$file+"_loading");
		if ($obj_loading.length > 0) {
			$obj_loading.show();
		}

		$.ajaxFileUpload({
			"url": $url,
			"secureuri": false,
			"fileElementId": $file,
			"dataType": "json",
			"data": {
				uploadpart: $file
			},
			async: this.async,
			beforeSend: function ($xmlobj) {},
			complete: function ($data) {},
			success: function ($data) {
				$json = eval($data);
				$response = $json.response;
				$result = $json.result;
				$imgthumb = $json.thumbimg;
				$imgurl = $json.uploadimg;
				$base64img = $json.base64img;
				//成功
				if ($response == "1") {
					if ($obj_loading.length > 0) {
						$obj_loading.hide();
					}
					$("#imgcode").val($base64img);
					$("#"+$file+"_preview").attr("src", $imgurl);
					//$(".img_clipbox").slideDown(400);
					$(".img_clipbox").show();
					//加载裁剪框
					$("#"+$file+"_preview").cropper({
						aspectRatio:110/135,
						width: 130,
						height: 169,
						mouseWheelZoom:false,
						touchDragZoom:false,
						crop: function(data) {
							$_x = data.x; //剪裁区域左侧的偏移
							$_y = data.y; //剪裁区域距上部的偏移
							$_cut_width = data.width; //剪裁区域的宽度
							$_cut_height = data.height; //剪裁区域的高度
							$("#x1").val($_x);
							$("#y1").val($_y);
							$("#w").val($_cut_width);
							$("#h").val($_cut_height);
						}
					});
				}
				//失败
				else {
					if ($obj_loading.length > 0) {
						$obj_loading.hide();
					}
					$error = "上传失败，请检查图片大小和格式！";
					if ($result.length > 0 ) {
						$error = $result;
					}
					if ($obj_tips.length > 0) {
						$obj_tips.html("<font color='red'>"+$error+"</font>");
					}
				}

			},
			error: function ($data, e, b) {
				if ($obj_tips.length > 0) {
					$obj_tips.html("<font color='red'>上传失败，请检查图片大小和格式！</font>");
				}

			}
		});

	});
	
	//关闭裁剪框
	$(document).on("click", ".img_clipbox_close", function(){
		//$(".img_clipbox").slideUp(400);
		//$(".img_clipbox").hide();
		window.location.reload();
	});


	//确定裁剪
	$(document).on("click", "[f='clipimg']", function(){
		$obj_cliptips = $("#clip_tips");
		$obj_cliptips.html("");

		$_imgcode = $("#imgcode").val();
		$_x1 = $("#x1").val();
		$_y1 = $("#y1").val();
		$_x2 = $("#x2").val();
		$_y2 = $("#y2").val();
		$_w = $("#w").val();
		$_h = $("#h").val();
		$_boundwidth = boundx; //压缩比例宽
		$_boundheight = boundy; //压缩比例高
		if ($_w.length == 0) {
			$obj_cliptips.html("请拖动设置白色裁剪框");
			return false;
		}
		if ($_h.length == 0) {
			$obj_cliptips.html("请拖动设置白色裁剪框");
			return false;
		}
		if ($_imgcode.length == 0) {
			$obj_cliptips.html("请先上传照片");
			return false;
		}
		$.ajax({
			type: "POST",
			url: _ROOT_PATH+"index.php?c=passport",
			cache: false,
			data: {a:"cropper", imgcode:$_imgcode, x1:$_x1, y1:$_y1, x2:$_x2, y2:$_y2, w:$_w, h:$_h, boundwidth:$_boundwidth,  boundheight:$_boundheight, r:get_rndnum(8)},
			dataType: "json",
			success: function($data) {
				$json = eval($data);
				$response = $json.response;
				$result = $json.result;
				$attid = $json.attid;
				if (typeof($attid) == "undefined") {
					$attid = "";
				}
				if ($response == "1") {
					$("#my_headimg_preview").attr("src", $result+"?r="+get_rndnum(8));
					$obj_cliptips.html("<font color='green'>裁剪成功</font>");
					_oeloveFileSynYun($attid); //同步云存储
					setTimeout(function(){
						window.location.href = _ROOT_PATH+"index.php?c=passport&a=success";
					}, 600);
				}
				else {
					$(".img_clipbox").hide();
					if ($result.length > 0) {
						ToastShow($result);
					}
					else {
						ToastShow("裁剪失败");
					}
				}
			},
			error: function() {
			}
		});
	});



});